/**  
 * @Title: FlowService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-03-23 15:09:58 
 */
package com.fsxgt.datagrid.flow.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fsxgt.datagrid.flow.bo.FlowComponentBO;
import com.fsxgt.datagrid.flow.bo.FlowInfoBO;
import com.fsxgt.datagrid.flow.bo.FlowMenuBO;
import com.fsxgt.datagrid.flow.dao.FlowInfoMapper;
import com.fsxgt.datagrid.flow.dao.FlowMenuMapper;
import com.fsxgt.datagrid.flow.exception.FlowException;
import com.fsxgt.datagrid.flow.service.IComponentService;

/**
 * @ClassName: FlowService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-03-23 15:09:58
 */
@Service
public class FlowMenuService {

	@Autowired
	private FlowMenuMapper flowMenuMapper;
	

	@Autowired
	private FlowInfoMapper flowInfoMapper;
	
	@Autowired
	private CmpRegistryService cmpRegistryService;
	

	/**  
	 * @Title: getMenu
	 * @Description: TODO(描述)
	 * @param id
	 * @author liuyi
	 * @param username 
	 * @return 
	 * @date 2023-04-03 13:49:05 
	 */  
	public List getMenuList() {
		
		
		List<FlowMenuBO> list = flowMenuMapper.findMenuByRoot();
		
		Map<String, List<FlowMenuBO>> parentIdMap = list.stream()
				.collect(Collectors.groupingBy(m -> (m.getParentId()==null?"":m.getParentId())));
		
		Map<String, FlowMenuBO> idMap =  list.stream().collect(Collectors.toMap(m -> (m.getId()), m -> (m)));

		
		for(FlowMenuBO menu:list) {
			menu.setType("menu");
			if(parentIdMap.containsKey(menu.getId())) {
				menu.getChildren().addAll(parentIdMap.get(menu.getId()));
			}
		}
		
		if(list.size()>0) {
			List<FlowMenuBO> flowList=flowMenuMapper.findFlowInfo(list);

			for(FlowMenuBO flow:flowList) {
				flow.setType("flow");
				idMap.get(flow.getParentId()).getChildren().add(flow);
				flow.getChildren().addAll(flowInfoMapper.findFlowComponentsByFlowId(flow.getId()));
			}
		}
		
		
		return parentIdMap.get("")!=null?parentIdMap.get(""):new ArrayList();

		
	}
	
	public String getFlowRootId(String flowInfoId) {
		String menuId = flowInfoMapper.findFlowInfoById(flowInfoId).getFlowMenuId();
		return flowMenuMapper.findRoot(menuId).getId();
	}


	/**  
	 * @Title: addFlowMenu
	 * @Description: TODO(描述)
	 * @param flowBO
	 * @author liuyi
	 * @throws FlowException 
	 * @date 2023-04-03 16:06:34 
	 */  
	@Transactional(rollbackFor = Exception.class)
	public void addFlowMenu(FlowMenuBO flowMenuBO) throws FlowException {
		// TODO Auto-generated method stub
		
		if(flowMenuBO.getParentId()==null) {
			throw new FlowException("上级不能为空");
		}
		
		if(flowMenuMapper.getFlowMenuById(flowMenuBO.getParentId())==null) {
			throw new FlowException("找不到上级节点");
		}
		if(flowMenuBO.getName()==null) {
			throw new FlowException("子类名不能为空");
		}
//		if(flowMenuMapper.getFlowMenuByName(flowMenuBO).size()>0) {
//			throw new FlowException("菜单名称不能重复");
//		}
		flowMenuBO.setId(UUID.randomUUID().toString());
		
		
		flowMenuMapper.insertFlowMenu(flowMenuBO);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void addFlowInfo(FlowInfoBO flowInfo) throws FlowException {
		// TODO Auto-generated method stub
		
		
		
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		if(flowInfo.getFlowMenuId()==null) {
			throw new FlowException("上级不能为空");
		}
		
		if(flowMenuMapper.getFlowMenuById(flowInfo.getFlowMenuId())==null) {
			throw new FlowException("找不到上级节点或上级节点不是子类");
		}
		if(flowInfo.getName()==null) {
			throw new FlowException("流程名称不能为空");
		}
		
		flowInfo.setCreateTime(new Date());
		
//		if(flowMenuMapper.getFlowMenuByName(flowMenuBO).size()>0) {
//			throw new FlowException("菜单名称不能重复");
//		}
		flowInfo.setId(UUID.randomUUID().toString());
		flowInfo.setUpdateTime(new Date());
				
		flowInfoMapper.insertFlowInfo(flowInfo);

	}
	
	

	/**  
	 * @Title: deleteFlow
	 * @Description: TODO(描述)
	 * @param flowMenuBO
	 * @author liuyi
	 * @throws FlowException 
	 * @date 2023-04-06 14:20:53 
	 */  
	@Transactional(rollbackFor = Exception.class)
	public void deleteFlowObject(FlowMenuBO flowMenuBO) throws FlowException {
		// TODO Auto-generated method stub
		
		if(flowMenuBO.getType().equals("menu")) {
			if(flowMenuMapper.getChildrenById(flowMenuBO.getId()).size()>0) {
				throw new FlowException("存在下级目录，不能删除");
			}
			flowMenuMapper.deleteFlowMenu(flowMenuBO.getId());
		}else if(flowMenuBO.getType().equals("flow")) {
			
			List<FlowComponentBO> list = flowInfoMapper.findFlowComponentsByFlowId(flowMenuBO.getId());
			
			for(FlowComponentBO bo:list) {
				IComponentService service = cmpRegistryService.getService(bo.getComponentType());
				service.deleteComponent(bo.getFlowInfoId(), bo.getComponentSeq());
			}
			
			flowInfoMapper.deleteFlowInfo(flowMenuBO.getId());
			flowInfoMapper.deleteFlowComponentsByFlowId(flowMenuBO.getId());
			flowInfoMapper.deleteFlowRelationsByFlowId(flowMenuBO.getId());
		}
		
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void updateFlowObject(FlowMenuBO flowMenuBO) throws FlowException {
		// TODO Auto-generated method stub
		
		if(flowMenuBO.getType().equals("menu")) {
			flowMenuMapper.updateFlowMenuName(flowMenuBO.getId(),flowMenuBO.getName());
		}else if(flowMenuBO.getType().equals("flow")) {
			flowInfoMapper.updateFlowInfoName(flowMenuBO.getId(),flowMenuBO.getName());
		}
		
	}

	/**  
	 * @Title: addSubject
	 * @Description: TODO(描述)
	 * @param flowSubjectBO
	 * @author liuyi
	 * @date 2023-09-25 13:40:22 
	 */  
	public FlowMenuBO addRootMenu(FlowMenuBO flowMenuBO) {
		// TODO Auto-generated method stub
		
		flowMenuBO.setId(UUID.randomUUID().toString());
		flowMenuBO.setParentId(null);
		flowMenuMapper.insertFlowMenu(flowMenuBO);
		
		return flowMenuBO;

	}
	



	
	

}
